package com.xw.llm.test;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.*;



public class DbMsUtil {
    public static String sendSql( ) throws Exception {
        RestTemplate restTemplate = new RestTemplate();

        String url = "https://db.fosun-creative.com/query/";

        HttpHeaders headers = new HttpHeaders();
        headers.set("accept", "application/json, text/javascript, */*; q=0.01");
        headers.set("accept-language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");
        headers.set("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.set("origin", "https://db.fosun-creative.com");
        headers.set("priority", "u=1, i");
        headers.set("referer", "https://db.fosun-creative.com/sqlquery/");
        headers.set("sec-ch-ua", "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"");
        headers.set("sec-ch-ua-mobile", "?0");
        headers.set("sec-ch-ua-platform", "\"Windows\"");
        headers.set("sec-fetch-dest", "empty");
        headers.set("sec-fetch-mode", "cors");
        headers.set("sec-fetch-site", "same-origin");
        headers.set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0");
        headers.set("x-csrftoken", "bI5yxfb0M09Ii4OLIyo3wZyd0qL7rfPQem1py2DslI2bGpNG0rpZMGOLnrhlQLzl");
        headers.set("x-requested-with", "XMLHttpRequest");
        headers.set("cookie", "X-FOSUN-TOKEN-BOSS_BG_MANAGE1=Sy9DV...; X-FOSUN-ORG_CODE-BOSS_BG_MANAGE1=00000001; Admin-Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...; csrftoken=bI5yxfb0M09Ii4OLIyo3wZyd0qL7rfPQem1py2DslI2bGpNG0rpZMGOLnrhlQLzl; sessionid=d7qfq546hwwtf06jri4304xl0kktzojz; acw_tc=0b32825a17542769613662115ef64d13a90dd5e6e61954ff4def1dc0807e5e");

        String body = "instance_name=WMS-prod&db_name=cc_gsp&schema_name=&tb_name=&sql_content=SELECT+*+from+flow_statistics+where+id+%3D+395515%3B&limit_num=100";

        HttpEntity<String> entity = new HttpEntity<>(body, headers);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        String mysqlResult = toMysqlResult(response.getBody());
        return mysqlResult;
    }

    public static String  toMysqlResult(String json) throws Exception {


        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(json);
        JsonNode data = root.get("data");
        List<String> columns = new ArrayList<>();
        for (JsonNode col : data.get("column_list")) {
            columns.add(col.asText());
        }
        List<List<Object>> rows = new ArrayList<>();
        for (JsonNode row : data.get("rows")) {
            List<Object> r = new ArrayList<>();
            for (JsonNode v : row) {
                if (v.isNull()) r.add(null);
                else if (v.isNumber()) r.add(v.numberValue());
                else r.add(v.asText());
            }
            rows.add(r);
        }

        // 模拟MySQL协议响应（这里只是打印，实际协议需用二进制包处理）
        System.out.println("MySQL ResultSet Columns:");
        System.out.println(columns);
        System.out.println("MySQL ResultSet Rows:");
        for (List<Object> r : rows) {
            System.out.println(r);
        }

        return JSON.toJSONString(columns);
    }
}